#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os

from PIL import Image, ImageSequence, ImageDraw, ImageFont


def txtlayer(base, text="Hello World!", xy=(0, 0), color="#FFFFFF"):
    # make a blank image for the text, initialized to transparent text color
    txt = base.copy()
    font_size = xy[1]//(len(text)+1)
    # get a font
    fnt = ImageFont.truetype(u'resources/站酷快乐体.ttf', font_size)
    text_width = fnt.getsize(text)
    print(xy)
    xy = int((xy[0]-text_width[0])/2), int((xy[1]-text_width[1]*1.3))
    print(xy)
    
    # get a drawing context
    d = ImageDraw.Draw(txt)

    # draw text, half opacity
    # d.text(xy, text, font=fnt, fill=(255, 255, 255, 255))
    d.multiline_text(xy, text, font=fnt, fill=color,  align="center")
    mytext(d,xy,text, font=fnt, fill=color)
    
    return txt

def convert(src='tmp\\demo\\2e9d7387cfb1ef83ba9814a99be8e3b9.png',text="我！可爱！懂么！", color="#FFFFFF"):

    with Image.open(src) as im:
        # get an image
        base = im.copy().convert('RGBA')
        xy=base.size
        txted = txtlayer(base, text, xy, color)
        
        txted.resize((240,240)).save(src.replace(".jpg",f".png").replace(".png",f".{text}.png").replace("yige",f"face"))
        print(txted)


def mytext(d, pos, text, font, fill, border='#FF0000', bp=3):
    x, y = pos
    shadowcolor = border
    d.text((x-bp, y), text, font=font, fill=shadowcolor)
    d.text((x+bp, y), text, font=font, fill=shadowcolor)
    d.text((x, y-bp), text, font=font, fill=shadowcolor)
    d.text((x, y+bp), text, font=font, fill=shadowcolor)
    # thicker border
    d.text((x-bp, y-bp), text, font=font, fill=shadowcolor)
    d.text((x+bp, y-bp), text, font=font, fill=shadowcolor)
    d.text((x-bp, y+bp), text, font=font, fill=shadowcolor)
    d.text((x+bp, y+bp), text, font=font, fill=shadowcolor)
    # now draw the text over it
    d.text((x, y), text, font=font, fill=fill)
    
def gui():
    from appJar import gui

    app = gui()
    # app.setFont(20)
    app.setGeometry("600x400")


    def select(f):
        a = app.openBox(title="aaa", dirName=os.getcwd(), fileTypes=[('images', '*.png'),('images', '*.jpg')], asFile=True, parent=None)
        app.setEntry(u"文件名", a.name)
        app.setEntry(u"话术", "你的小可爱突然出现")
        im = Image.open(a.name)
        x, y = im.size
        app.setEntry(u"文字坐标x", x)
        app.setEntry(u"文字坐标y", y)


    app.startLabelFrame("pic_group", hideTitle=True)
    app.addButton(u"选择", select)
    app.addLabelEntry(u"文件名")
    app.addLabelEntry(u"文字坐标x")
    app.addLabelEntry(u"文字坐标y")
    app.setEntry(u"文字坐标x", "10")
    app.setEntry(u"文字坐标y", "10")
    app.stopLabelFrame()


    def selectcolor(f):
        color = app.colourBox(colour="#FF0000")
        print(color)
        app.setEntry(u"color", color)


    app.startLabelFrame("color_group", hideTitle=True)
    app.addButton("选择color", selectcolor)

    app.addLabelEntry(u"color")
    app.setEntry(u"color", "#FFFFFF")
    app.stopLabelFrame()

    app.addLabelEntry(u"话术")


    def gene(f):
        print(f)
        giffile = app.getEntry(u"文件名")
        x = app.getEntry(u"文字坐标x")
        y = app.getEntry(u"文字坐标y")
        text = app.getEntry(u"话术")
        xy = (int(x), int(y))
        color = app.getEntry(u"color")
        convert(giffile,text, color)
        app.infoBox("消息", "生成成功")


    app.addButton("生成", gene)

    app.go()

if __name__ == '__main__':
    # convert(text="要猫猫一直委屈吗")
    with open("resources\\20230215162538.txt",encoding="utf-8") as f:
        for line in f.readlines()[:30]:
            if line.strip() != "" and len(line.strip())<10:
                convert(text=line.strip())
    

